﻿using System.Collections.Generic;
using Hidistro.Core;
using Hidistro.Core.Enums;
using Hidistro.Entities;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using wnxd_feature;
using System;

namespace Hidistro.UI.SaleSystem.Tags
{

    public class wnxd_SubPage_Sort : WebControl
    {

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            int categoryId = int.Parse((HttpContext.Current.Handler as System.Web.UI.Page).Request.QueryString["CategoryId"]);
            CategoryInfo category = CategoryBrowser.GetCategory(categoryId);
            List<CategoryInfo> list = CategoryBrowser.GetMaxSubCategories(category.CategoryId, 0x3e8) as List<CategoryInfo>;
            int i = 0;
            feature wnxd = new feature();
            foreach (CategoryInfo ca in list)
            {
                builder.AppendFormat("<div class=\"title-js\">{0}", (++i) + "F —" + ca.Name);
                builder.Append("<div class=\"title-js-one\">");
                string[] wnxd_feature = wnxd.GetFeature(ca.CategoryId);
                string url = Globals.GetSiteUrls().SubCategory(ca.CategoryId, ca.RewriteName, ca.ParentRewriteName);
                for (int n = 0; n < wnxd_feature.Length; n++)
                {
                    if (!string.IsNullOrEmpty(wnxd_feature[n]))
                    {
                        builder.AppendFormat("<a href=\"{1}\" target=\"_blank\">{0}</a>", wnxd_feature[n], url + "?feature=" + (n + 1));
                    }
                }
                builder.Append("</div><div class=\"title-js-two\">");
                builder.AppendFormat("<a href=\"{0}\" target=\"_blank\"><img src=\"http://image.juseshop.com/UploadImage/AdvertImg/js-img92.jpg\" border=\"0\"></a>", Globals.GetSiteUrls().SubCategory(ca.CategoryId, ca.RewriteName, ca.ParentRewriteName));
                builder.Append("</div></div>");
                DataTable productList = this.GetRandomList(ca.CategoryId, 5);
                builder.Append("<div class=\"content9-js\">");
                if ((productList != null) && (productList.Rows.Count > 0))
                {
                    foreach (DataRow row in productList.Rows)
                    {
                        builder.Append("<div class=\"content9-box\">");
                        string title = string.Empty;
                        string alt = string.Empty;
                        string keyword = row["Meta_Keywords"].ToString();
                        if (!string.IsNullOrEmpty(keyword))
                        {
                            string[] arr = keyword.Split('|');
                            if (arr.Length == 3)
                            {
                                title = arr[1];
                                alt = arr[2];
                            }
                        }
                        builder.AppendFormat("<a href=\"{1}\" target=\"_blank\"><img src=\"{0}\" width=\"235px\" height=\"235px\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                        builder.AppendFormat("<p><a href=\"{1}\" alt=\"{0}\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                        if (row["ProductText"] != DBNull.Value) builder.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\" target=\"_blank\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                        if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                        builder.AppendFormat("</p><p><b>￥{0}</b></p>", Globals.FormatMoney((decimal)row["RankPrice"]));
                        builder.Append("</div>");
                    }
                }
                builder.Append("</div><div class=\"content8-js\">");
                StringBuilder builder1 = new StringBuilder();
                StringBuilder builder2 = new StringBuilder();
                DataTable brandCategories = this.GetProductList(ca.CategoryId, 9);
                builder1.Append("<div class=\"content8-left\">");
                builder2.Append("<div class=\"content8-right\">");
                if ((brandCategories != null) && (brandCategories.Rows.Count > 0))
                {
                    bool first = true;
                    foreach (DataRow row in brandCategories.Rows)
                    {
                        string title = string.Empty;
                        string alt = string.Empty;
                        string keyword = row["Meta_Keywords"].ToString();
                        if (!string.IsNullOrEmpty(keyword))
                        {
                            string[] arr = keyword.Split('|');
                            if (arr.Length == 3)
                            {
                                title = arr[1];
                                alt = arr[2];
                            }
                        }
                        if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                        if (first)
                        {
                            first = false;
                            builder1.AppendFormat("<a href=\"{0}\" target=\"_blank\"><img src=\"{1}\" width=\"240px\" height=\"240px\" title=\"{2}\" alt=\"{3}\">", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), title, alt);
                            builder1.AppendFormat("<p><a title=\"{0}\" href=\"{1}\" style=\"color:#333333\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                            if (row["ProductText"] != DBNull.Value) builder1.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\" target=\"_blank\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                            builder1.AppendFormat("</p><p><b>￥{0}</b><i>包邮</i></p></a>", Globals.FormatMoney((decimal)row["RankPrice"]));
                        }
                        else
                        {
                            builder2.Append("<div class=\"pro8\">");
                            builder2.AppendFormat("<a href=\"{0}\" target=\"_blank\"><img src=\"{1}\" width=\"110px\" height=\"110px\" title=\"{2}\" alt=\"{3}\">", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), title, alt);
                            builder2.AppendFormat("<p class='wnxd-text'><a title=\"{0}\" href=\"{1}\" style=\"color:#333333\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                            if (row["ProductText"] != DBNull.Value) builder2.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\" target=\"_blank\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                            builder2.AppendFormat("</p><p><b>￥{0}</b><i>包邮</i></p></a>", Globals.FormatMoney((decimal)row["RankPrice"]));
                            builder2.Append("</div>");
                        }
                    }
                }
                builder1.Append("</div>");
                builder2.Append("</div>");
                builder.Append(builder1.ToString() + builder2.ToString());
                builder.Append("</div>");
            }
            return builder.ToString();
        }
        private DataTable GetRandomList(int categoryid, int MaxNum)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "NewID()",
                CategoryIds = categoryid.ToString(),
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }
        private DataTable GetProductList(int categoryid, int MaxNum)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "ShowSaleCounts",
                SortOrder = SortAction.Desc,
                CategoryIds = categoryid.ToString(),
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }
    }
}